
//895.最大频率栈
class FreqStack {
    vector<pair<int,int>> nums;   //存储每一个元素,以及出现的次数
    int max_count=0;            //存储最大出现次数
public:
    FreqStack() {}
    
    void push(int val) {
        int count=0;
        for(auto& [x,y]:nums) 
        if(x==val) y++,count++;     //将数组中等于val的值个数+1

        nums.push_back({val,count+1});
        max_count=max(max_count,count+1);
    }
    
    int pop() {
        for(auto it=nums.rbegin();it!=nums.rend();it++)
        {
            if(it->second==max_count)  //找到了要进行删除的元素
            {
                int ret=it->first;
                nums.erase((++it).base());    //将其进行删除

                max_count=0;
                for(auto& [x,y]:nums)  //将nums中的元素的数目进行改变
                {
                    if(x==ret) y--;
                    max_count=max(max_count,y);
                }

                return ret;
            }
        }
        return -1;     
    }
};

/**
 * Your FreqStack object will be instantiated and called as such:
 * FreqStack* obj = new FreqStack();
 * obj->push(val);
 * int param_2 = obj->pop();
 */